package com.cuit.zuke.consumer.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cuit.zuke.common.util.Result;
import com.cuit.zuke.consumer.pojo.User;
import com.cuit.zuke.consumer.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("all")
@RestController
@RequestMapping("/consumer/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    /**
     * 查询用户
     * @param currentPage 当前页
     * @param pageSize    每页展示数
     * @return
     */
    @RequestMapping("/list")
    public Map<String,Object> queryUserList(int currentPage, int pageSize) {
        currentPage = (currentPage-1)*pageSize;
        System.out.println(currentPage+" "+pageSize);
        List<User> users = userService.queryUserList(currentPage, pageSize);
        int total = userService.queryTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("list", users);
        map.put("total", total);
        return map;
    }

    /**
     * 根据用户id查询用户
     * @param userId
     * @return 后台无法接受对象，用List来包装对象
     */
    @RequestMapping("/queryUserById")
    public List<User> queryUserById(String userId) {

        System.out.println(userId);
        User user = userService.queryUserById(userId);
        List<User> userList = new ArrayList<>();
        userList.add(user);
        return userList;
    }

    /**
     * 修改来用户状态
     * @param userId
     * @return
     */
    @RequestMapping("/update")
    public int updateStatus(String userId) {
        User user = userService.queryUserById(userId);
        if(user.getUserStatus() == 1) {
            userService.updateStatus(userId, 0);
        }else {
            userService.updateStatus(userId, 1);
        }
//        System.out.println(user.getUserStatus());
        return 0;
    }

    /**
     * 批量修改用户状态
     * @param userList 传回来的用户id数组
     * @return
     */
    @RequestMapping("/multipleUpdate")
    public int multipleUpdate(@RequestBody Long[] userList) {
        User user;
        for (Long aLong : userList) {
//            System.out.println(aLong);
            String userId = String.valueOf(aLong);
            user = userService.queryUserById(userId);
            if(user.getUserStatus() == 1) {
                userService.updateStatus(userId, 0);
            }else {
                userService.updateStatus(userId, 1);
            }
        }
        return 0;
    }

    /**
     * 用户登录
     * @param userPhone
     * @param password
     * @return
     */
    @PostMapping("/userLogin")
    public User login(@RequestParam("userPhone") String userPhone, @RequestParam("password") String password) {
//        System.out.println("userPhone=>"+ userPhone +" password=>"+password);
        User user = userService.queryUserByPhone(userPhone);
//        System.out.println(user);
        if(user != null) {
            String userPassword = user.getUserPassword();
            if(userPassword.equals(password)) {
                return user;
            }else{
                return null;
            }
        }else {
            return null;
        }
    }

    @PostMapping("/userRegister")
    public User register(@RequestParam("userPhone") String userPhone, @RequestParam("password") String password) {
        try {
            System.out.println("userPhone=>" + userPhone + " password=>" + password);
            User user = userService.queryUserByPhone(userPhone);
            System.out.println("user=>" + user);
            if (user == null) {
                String userId = userService.queryMaxUserId();
                userId = String.valueOf(Integer.parseInt(userId) + 1);
                String userName = "用户" + userId;
//                System.out.println(userName);
                Date date = new Date();
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(sdf.format(date));
                userService.insertUser(userId, userPhone, password, 1, userName,sdf.format(date));
                user = userService.queryUserByPhone(userPhone);
                return user;
            } else {
                return null;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/updateUserInfo")
    public User updateUserInfo(@RequestParam("userJson") String userJson) {
        System.out.println("进入方法");
        System.out.println(userJson);
        User user = JSONObject.parseObject(userJson, User.class);
        System.out.println(user);
        userService.updateUer(user);
        user = userService.queryUserByPhone(user.getUserPhone());
        return user;
    }


    /**
     * 用于服务调用的接口
     * @param userId
     * @return
     */
    @PostMapping("getUserByIdCli")
    public User getUserById(@RequestBody String userId){
        User user = userService.queryUserById(userId);
        return user;
    }

    @GetMapping("/getDailyUserData")
    public Map<String, List<?>> getDailyUserData () {
        return userService.getDailyUserData();
    }

    @GetMapping("/getWeeklyAdminLoginAndUserRegData")
    public Result getWeeklyUserRegData () {
        return userService.getWeeklyUserRegData();
    }

}
